package com.zp.mall.product.controller;

import java.util.*;

import com.zp.mall.product.entity.AttrEntity;
import com.zp.mall.product.entity.CategoryEntity;
import com.zp.mall.product.service.AttrAttrgroupRelationService;
import com.zp.mall.product.service.AttrService;
import com.zp.mall.product.service.CategoryService;
import com.zp.mall.product.vo.AttrGroupRelationVo;
import com.zp.mall.product.vo.AttrGroupWithAttrsVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.zp.mall.product.entity.AttrGroupEntity;
import com.zp.mall.product.service.AttrGroupService;
import com.zp.common.utils.PageUtils;
import com.zp.common.utils.R;



/**
 * 属性分组
 *
 * @author zhangpeng
 * @email zhangpeng@aliyun.com
 * @date 2020-10-11 17:08:57
 */
@RestController
@RequestMapping("product/attrgroup")
public class AttrGroupController {
    @Autowired
    private AttrGroupService attrGroupService;
    @Autowired
    private CategoryService categoryService;
    @Autowired
    private AttrService attrService;

    //  /product/attrgroup/{attrgroupId}/attr/relation  获取属性分组关联的所有属性
    @GetMapping("/{attrgroupId}/attr/relation")
    public R attrRelation(@PathVariable("attrgroupId") Long attrgroupId){
        List<AttrEntity> attrEntities = attrService.getAttrsByAttrgroupId(attrgroupId);
        return R.ok().put ("data", attrEntities);
    }


    // /product/attrgroup/attr/relation/delete  删除属性与分组的关联关系
    @PostMapping("/attr/relation/delete")
    public R deleteRelation(@RequestBody List<AttrGroupRelationVo> attrGroupRelationVos){
        attrGroupService.deleteRelation(attrGroupRelationVos);
        return R.ok ();
    }


    //  /product/attrgroup/{attrgroupId}/noattr/relation   获取属性分组没有关联的其他属性
    @GetMapping("/{attrgroupId}/noattr/relation")
    public R noAttrList(@RequestParam Map<String, Object> params, @PathVariable("attrgroupId") Long attrGroupId){
        PageUtils page = attrGroupService.getNoAttrs(params, attrGroupId);
        return R.ok().put("page", page);
    }

    /**
     * 列表  可以根据类别Id查询其下的属性分组
     */
    @RequestMapping("/list/{catelogId}")
    //@RequiresPermissions("product:attrgroup:list")
    public R list(@RequestParam Map<String, Object> params, @PathVariable("catelogId") Long catelogId){
//        PageUtils page = attrGroupService.queryPage(params);
        PageUtils page = attrGroupService.queryPage(params, catelogId);

        return R.ok().put("page", page);
    }

    // /product/attrgroup/attr/relation  添加属性与分组关联关系
    @PostMapping("/attr/relation")
    public R addAttrRelation(@RequestBody List<AttrGroupRelationVo> vos){
        attrGroupService.addAttrRelation(vos);
        return R.ok ();
    }

    /**
     * /product/attrgroup/{catelogId}/withattr
     * 获取分类下的属性分组以及属性
     */
    @GetMapping("/{catelogId}/withattr")
    public R getAttrGroupWithAttrsByCatId(@PathVariable("catelogId") Long catelogId){
        List<AttrGroupWithAttrsVo> vos = attrGroupService.getAttrGroupWithAttrsByCatId(catelogId);

        return R.ok ().put ("data", vos);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{attrGroupId}")
    //@RequiresPermissions("product:attrgroup:info")
    public R info(@PathVariable("attrGroupId") Long attrGroupId){
		AttrGroupEntity attrGroup = attrGroupService.getById(attrGroupId);
		Long[] path = categoryService.findCatelogPath(attrGroup.getCatelogId ());
        attrGroup.setCatelogPath (path);

        return R.ok().put("attrGroup", attrGroup);
    }

    private List<Long> getParenPath(CategoryEntity categoryEntity, List<Long> catelogPath) {
        catelogPath.add (categoryEntity.getCatId ());
        if (categoryEntity.getParentCid () != 0){
            CategoryEntity byId = categoryService.getById (categoryEntity.getParentCid ());
            getParenPath (byId, catelogPath);
        }
        return catelogPath;
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("product:attrgroup:save")
    public R save(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.save(attrGroup);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("product:attrgroup:update")
    public R update(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.updateById(attrGroup);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("product:attrgroup:delete")
    public R delete(@RequestBody Long[] attrGroupIds){
		attrGroupService.removeByIds(Arrays.asList(attrGroupIds));

        return R.ok();
    }

}
